package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Banner;
import com.jyy.prefabricated.service.BannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 轮播图
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 新增banner
     * @param banner
     * @return
     */
    @PostMapping("/banner")
    public AjaxResult insert(@Valid @RequestBody Banner banner){
        log.info("Rest request to insert banner:{}",banner);
        int id = bannerService.insert(banner);
        Banner result = bannerService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新banner
     * @param banner
     * @return
     */
    @PutMapping("/banner")
    public AjaxResult update(@Valid @RequestBody Banner banner){
        log.info("Rest request to update banner:{}",banner);
        bannerService.update(banner);
        Banner result = bannerService.selectById(banner.getId());
        return AjaxResult.success(result);
    }

    /**
     * 切换banner状态
     * @param id
     * @return
     */
    @PutMapping("/banner-status/{id}")
    public AjaxResult updateStatus(@PathVariable Integer id){
        log.info("Rest request to update banner status by id:{}",id);
        int row = bannerService.updateStatus(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 批量更新banner状态
     * @param dto
     * @return
     */
    @PutMapping("/banner-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update banner:{}",dto);
        bannerService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除banner
     * @param dto
     * @return
     */
    @DeleteMapping("/banner-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete banner by id:{}",dto);
        int row = bannerService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询banner详情
     * @param id
     * @return
     */
    @GetMapping("/banner/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find banner by id:{}",id);
        Banner result = bannerService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除banner
     * @param id
     * @return
     */
    @DeleteMapping("/banner/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete banner by id:{}",id);
        int row = bannerService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询banner
     * @param title
     * @param status
     * @param pageable
     * @return
     */
    @GetMapping("/banner")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find banner by params title:{},status:{}", title, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Banner> banner = bannerService.selectByParams(title, status);
        PageInfo<Banner> result = new PageInfo<>(banner);
        return AjaxResult.success(result);
    }

    /**
     * 查询banner列表
     * @param title
     * @param status
     * @return
     */
    @GetMapping("/banner-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status){
        log.info("Rest request to find banner by params title:{},status:{}", title, status);
        List<Banner> result = bannerService.selectByParams(title, status);
        return AjaxResult.success(result);
    }

}
